<div class="events form">
	<?php $html->css('/events/css/events.css',null,array(),false);?>

<?php
$javascript->link('/cms/js/jquery.qtip.js',false);
$javascript->link('/js/mustache.js',false);

$presentUrl = $this->params['url']['url'];
$eventAssetStatus = 1;
$user_id = $session->read('Auth.User.id');
$isAdmin = false;
if(isset($this->params['admin']) && $this->params['admin'])
{
	$isAdmin = true;
	$user_id = null;
}
	
echo $form->create('Event',array('url'=>'/'.$presentUrl,'enctype'=>'multipart/form-data'));
//echo $form->inputs(array('fieldset'=>false), null, 'add');
$fields = cri("Events.Event")->getFields("group","add");
$eventCostFieldsFlag = false;
foreach ($fields as $field)
{
	echo $eventForm->showInputField($form,$field);
}

echo $form->input('Event.user_id',array('type'=>'hidden','value'=>$user_id));
echo $form->input('Event.id',array('type'=>'hidden'));

echo $form->input('EventAsset.0.name',array('type'=>'file','label'=>__('Upload Image'),'class'=>'required'));
echo $form->input('EventAsset.0.user_id',array('type'=>'hidden','value'=>$user_id,'class'=>'required'));
echo $form->input('EventAsset.0.status',array('type'=>'hidden','value'=>$eventAssetStatus,'class'=>'required'));
echo $form->input('EventAsset.0.is_default',array('type'=>'hidden','value'=>1,'class'=>'required'));
?>
<div id='place_holder'></div>
<?php
echo $html->link(__('Add more image'),'#',array('onclick'=>'return add_upload_field()', 'class'=>'addAnotherField'));

echo $form->end(__('Save'));
?>
</div>
<script type='text/javascript'>
<?php if(!isset($listingId) || !$listingId) { ?>
	$(document).ready(function()
	{
		$('#EventListingText').autocomplete("<?php echo $html->url("/events/events/suggestListings/")?>",{
			minChars: 2,
			//cacheLength: 10,
			onItemSelect: selectItem,
			//autoFill: false
		});
	});
<?php } ?>

	if (<?php echo json_encode($isFreeDefaultValue); ?> || $("#EventIsFree").attr('checked'))
	{
		document.getElementById('EventIsFree').checked = true;
		document.getElementById('EventEventCost').value = 0;
		document.getElementById('EventEventCostTo').value = 0;
		document.getElementById('EventEventCost').disabled = true;
		document.getElementById('EventEventCostTo').disabled = true;
	}
	
	var tmpObj = new templateClass(1);
	var user_id = '<?php echo $user_id ?>';
	var eventStatus = <?php echo $eventAssetStatus ?>;
	var defaultTemplate = '<input type="file" id="EventAsset{{i}}Name" value="" name="data[EventAsset][{{i}}][name]">';
	defaultTemplate += '<input type="hidden" id="EventAsset{{i}}UserId" value='+user_id+' name="data[EventAsset][{{i}}][user_id]">';
	defaultTemplate += '<input type="hidden" id="EventAsset{{i}}Status" value='+eventStatus+' name="data[EventAsset][{{i}}][status]">';

	function templateClass(count)
	{
		this.count = count;
	}
	
	function add_upload_field()
	{
		var tmpToAppend = Mustache.to_html(defaultTemplate,{'i':tmpObj.count});
		$('#place_holder').append(tmpToAppend);
		tmpObj.count ++;
		return false;
	}
	
	function selectItem(li) 
	{
		var sValue = null;	 
	    if( li == null ) 
		{
	    	sValue = null;
		} else {
		    if( !!li.extra )
		    {
			    sValue = li.extra[0];
		    }
		}
		document.getElementById('EventListingId').value=sValue;
	}
</script>